package com.example.aspect;

import jdk.nashorn.internal.runtime.regexp.joni.constants.OPCode;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class MyAspect implements Ordered {
    @Pointcut("@annotation(com.example.aspect.MyComment)")
    public void point(){

    }
    @Before("point()")
    public void methodBefore(JoinPoint joinPoint){
        Object[] args = joinPoint.getArgs();
        Object target = joinPoint.getTarget();
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();

        System.out.println("Before");
    }

    @AfterReturning(value = "point()",returning = "ret")
    public void methodAfterReturning(JoinPoint joinPoint, Object ret){
        System.out.println("AfterReturning");
    }

    @After("point()")
    public void methodAfter(){
        System.out.println("After");
    }

    @AfterThrowing(value = "point()",throwing = "e")
    public void methodAfterThrowing(JoinPoint joinPoint,Throwable e){
        System.out.println("AfterThrowing");
    }

    @Around("point()")
    public void methodAround(ProceedingJoinPoint joinPoint){
        System.out.println("方法执行前");
        try {
            joinPoint.proceed();
            System.out.println("方法执行后");
        } catch (Throwable e) {
            System.out.println("方法出现异常");
            throw new RuntimeException(e);
        }finally {
            System.out.println("finally进行增强");
        }
    }

    @Override
    public int getOrder() {
        return 1;
    }
}